//////////////////////////////////////////////////////////////////////
// MsgRouter.h
//
// SHEN Fangyang
// me@shenfy.com
//
// Copyright (C) SHEN Fangyang, 2011, All rights reserved.
//////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////
//  Brief Description
//
//	Message struct and 
//	message router class for dispatching game messages.
//
//////////////////////////////////////////////////////////////////////
#pragma once
#include <string>
#include <boost/unordered_map.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/shared_array.hpp>
#include <list>
#include <windows.h>
#include <Timer.h>

namespace grflib
{
	namespace engine
	{
		namespace AI
		{
			//PS. Message class is responsible for releasing package memory itself
			struct CMsg
			{
				CMsg();
				~CMsg();
				CMsg &operator= (const CMsg& msg);

				std::string m_id;
				std::string m_receiverId;
				boost::shared_array<char> m_package;
				double m_deliveryTime;
			};

			class CMsgRouter
			{
			public:
				~CMsgRouter();

				static CMsgRouter &Instance(void);

				//add msg to queue
				void SendMsg(const char *receiverId, const char *msgId, boost::shared_array<char> &pacakage, double delayInMS);

				//dispatch messages in its queue
				void Dispatch(void);

				//query router time
				double GetTime(void);

			private:
				//singleton, hide ctor
				CMsgRouter();
				typedef std::list<boost::shared_ptr<CMsg>> msg_queue_type;
			
			protected:
				msg_queue_type m_msgQueue;
				CRITICAL_SECTION m_cs;
				w32lib::CTimer m_timer;
			};

		} //namespace AI
	} //namespace engine
} //namespace grflib